Passed
Pull Request — develop (#758)
by Kevin Van
10:13 queued 04:10
created

Matches.tsx ➔ getData   C

Complexity

Conditions 9

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 3
rs 6.6666
c 0
b 0
f 0
cc 9
1
import axios from "axios"
2
import classNames from "classnames"
3
import { Link, graphql, useStaticQuery } from "gatsby"
4
import moment from "moment-timezone"
5
import "moment-timezone/node_modules/moment/locale/nl-be"
6
import React, { FunctionComponent, useEffect, useState } from "react"
7
import LazyLoad from "react-lazyload"
8
9
import { mapPsdStatus, mapPsdStatusShort } from "../scripts/helper"
10
import Icon from "./Icon"
11
import "./Matches.scss"
12
import Spinner from "./Spinner"
13
14
const MatchesRow: FunctionComponent<MatchesRowProps> = ({ match }: MatchesRowProps) => {
15
  moment.tz.setDefault(`Europe/Brussels`)
16
  moment.locale(`nl-be`)
17
  moment.localeData(`nl-be`)
18
19
  const d = moment(match.date)
20
  const date = d.format(`dddd D MMMM YYYY`)
21
  const time = d.format(`HH:mm`)
22
  const matchPlayed =
23
    ((match.status === 0 || match.status === null) && match.goalsHomeTeam !== null && match.goalsAwayTeam !== null) ||
24
    false
25
26
  return (
27
    <article>
28
      <header className="matches__calendar__title">
29
        <h3>
30
          <span className="matches__calendar__date">{date}</span>
31
          <span className="matches__calendar__separator">|</span>
32
          <span className="matches__calendar__type">{match.competitionType}</span>
33
        </h3>
34
      </header>
35
      <main className="matches__calendar__main">
36
        <div
37
          className={classNames(`matches__calendar__team`, `matches__calendar__team--home`, {
38
            "matches__calendar__team--winner": matchPlayed && match.goalsHomeTeam > match.goalsAwayTeam,
39
          })}
40
        >
41
          {match.homeClub?.abbreviation || match.homeClub?.name}
42
          <LazyLoad debounce={false}>
43
            <img
44
              src={match.homeClub?.logo}
45
              alt={match.homeClub?.name}
46
              className="matches__calendar__logo matches__calendar__logo--home"
47
            />
48
          </LazyLoad>
49
        </div>
50
51
        <div className="matches__calendar__score">
52
          {match.status !== 0 && (
53
            <span title={mapPsdStatus(match.status) || ``}>{mapPsdStatusShort(match.status)}</span>
54
          )}
55
          {(match.status === 0 || match.status === null) && !matchPlayed && <span>{time}</span>}
56
          {matchPlayed && (
57
            <span>
58
              {match.goalsHomeTeam} - {match.goalsAwayTeam}
59
            </span>
60
          )}
61
        </div>
62
63
        <div
64
          className={classNames(`matches__calendar__team`, `matches__calendar__team--away`, {
65
            "matches__calendar__team--winner": matchPlayed && match.goalsHomeTeam < match.goalsAwayTeam,
66
          })}
67
        >
68
          <LazyLoad debounce={false}>
69
            <img
70
              src={match.awayClub?.logo}
71
              alt={match.awayClub?.name}
72
              className="matches__calendar__logo matches__calendar__logo--away"
73
            />
74
          </LazyLoad>
75
76
          {match.awayClub?.abbreviation || match.awayClub?.name}
77
        </div>
78
79
        <Link to={`/game/${match.id}`} className="matches__calendar__link">
80
          <Icon icon="fa-info-circle" />
81
        </Link>
82
      </main>
83
    </article>
84
  )
85
}
86
87
const Matches: FunctionComponent<MatchesProps> = ({ teamId }: MatchesProps) => {
88
  const [data, setData] = useState<Match[]>([])
89
90
  const {
91
    site: {
92
      siteMetadata: { kcvvPsdApi },
93
    },
94
  }: MatchesQueryData = useStaticQuery(graphql`
95
    {
96
      site {
97
        siteMetadata {
98
          kcvvPsdApi
99
        }
100
      }
101
    }
102
  `)
103
104
  useEffect(() => {
105
    async function getData() {
106
      const response = await axios.get(`${kcvvPsdApi}/matches/${teamId}`)
107
      setData(response.data)
108
    }
109
    getData()
110
  }, [kcvvPsdApi, teamId])
111
112
  return (
113
    <div className={`matches__wrapper`}>
114
      {data.length > 0 || <Spinner />}
115
      {data
116
        .sort((a, b) => a.timestamp - b.timestamp)
117
        .map((match, i) => (
118
          <MatchesRow match={match} key={i} />
119
        ))}
120
    </div>
121
  )
122
}
123
124
export default Matches
125